<template>
	<view class="addressContent">
		<view class="addressLeft">

			<!-- 地址信息 -->
			<view class="addressInfo">
				<view class="addAddress"></view>
				<view v-if="addresssInfo.length != 0">
					<view class="phone"><span>{{addresssInfo.fullName}}</span>{{addresssInfo.mobile}}</view>
					<view class="address">
						{{addresssInfo.provinceText}}{{addresssInfo.cityText}}{{addresssInfo.areaText}}{{addresssInfo.address}}
					</view>
				</view>
				<!-- 如果没有地址 -->
				<view class='emptyAddress' v-else>
					<view class="addressTips">新建收货地址</view>
				</view>
			</view>
		</view>
		<!-- 右侧箭头 -->
		<image src="../../static/shopping/right.png" class="rightImg" @tap="goAddressList"></image>
	</view>
</template>

<script>
	import {
		addressList
	} from "@/api/api.js"
	export default {
		data() {
			return {
				// 地址列表
				addressList: [],
				addresssInfo: {},
				// 地址id
				addressId: '',
			}
		},
		props:{
			
		},
		
		mounted() {
			this.getAddressList()
		},
		onShow() {
			
		},
		methods: {
			// 前往地址列表
			goAddressList() {
				uni.navigateTo({
					url: '/pages/goods/addressList?isSubmit=1'
				})
			},
			// 获取地址列表
			getAddressList() {
				let data = {}
				addressList(data).then(res => {
					this.addressList = res.data
					// 如果没有添加地址,提示去添加
					if (this.addressList.length == 0) {
						this.addresssInfo = ''
					} else if (this.addressList.length == 1) {
						// 如果只有一个地址,直接显示为默认
						this.addresssInfo = this.addressList[0]
						this.addressId = this.addressList[0].id
					} else if (this.addressList.length > 1) {
						// 如果存在多个地址,先判断是否有默认地址
						let isHaveDefault = this.addressList.some(item => item.isDefault == 1)
						// 如果有,查询默认地址
						if (isHaveDefault) {
							this.addressList.forEach(item => {
								if (item.isDefault == 1) {
									this.addresssInfo = item
									this.addressId = item.id
								}
							})
						} else {
							// 如果没有,默认第一条地址
							this.addresssInfo = this.addressList[0]
							this.addressId = this.addressList[0].id
						}
					}
				})
			},
		}
	}
</script>

<style scoped>
	.addressContent {
		width: 100%;
		padding: 30rpx 20rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #FFFFFF;
		border-radius: 20rpx;

	}

	.addressContent .addressLeft .addressInfo {
		display: flex;
		align-items: center;
	}

	.addressContent .addressLeft .addressInfo .phone {
		font-size: 28rpx;
		font-family: PingFangSC-Bold, PingFang SC;
		font-weight: bold;
		color: #1A1A1A;

	}

	.addressContent .addressLeft .addressInfo .phone>span {
		margin-right: 24rpx;
	}

	.addressContent .addressLeft .addressInfo .address {
		margin-top: 20rpx;
		font-size: 26rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #1A1A1A;
	}

	.addressContent .addressLeft .addressInfo .emptyAddress {
		display: flex;
		align-items: center;
	}

	.addressContent .addAddress {
		width: 48rpx;
		height: 48rpx;
		background: url('@/static/statc-icon/image-4.png') no-repeat;
		background-size: 100% 100%;
		margin-right: 20rpx;
	}


	.addressContent .rightImg {
		width: 20rpx;
		height: 30rpx;
	}
</style>